/**
 * Created by AMD on 18.11.14.
 */
package model {
import InterfaceObjects.BottomPanel.PaintItem;

import messages.DebugMessage;

import model.objects.ShopDesignData;

import model.prototypes.ProductProto;
import model.prototypes.ShopDesignItemProto;
import model.prototypes.ShopObjectProto;
import model.prototypes.ShelfProto;

import mvcexpress.mvc.Proxy;

//-----------------------------------------------------------------------------------------------
// В этом прокси будет храниться информация о
// параметрах всех объектов в игре
//-----------------------------------------------------------------------------------------------


public class PrototypeProxy extends Proxy{
    //private var _productPrototypes:Object = null;
    private var _productPrototypesVector:Vector.<ProductProto> = null;
    private var _shelfPrototypesVector:Vector.<ShelfProto> = null;
    private var _shopObjectPrototypes:Object = null;
    private var _floorsPrototypesVector:Vector.<ShopDesignItemProto> = null
    private var _wallsPrototypesVector:Vector.<ShopDesignItemProto> = null


    public function PrototypeProxy() {
    }


    //---------------------------------------------
    //Products prototypes
    //---------------------------------------------

    public function get productsPrototypes():Vector.<ProductProto> {
        return _productPrototypesVector;
    }

    private var _o:Object;
    private var _bufName:String;
    public function set productsPrototype(value:Object):void {
        if(this._productPrototypesVector == null){
            _productPrototypesVector = new <ProductProto>[];
            var _protoIdPointer:int = 1;
            while(value["p"+_protoIdPointer] != null){
                this._bufName = "p"+_protoIdPointer;
                this._o = value[_bufName];
                this._productPrototypesVector.push(new ProductProto(_bufName,_o.sprite_id,_o.level,_o.price,_o.group,_o.volume,_o.demand,_o.deliver,_o.exp));
                _protoIdPointer ++;
            }
        } else {
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: trying to reinitialize productPrototypes")
        }
    }
    ///{name:"Water", sprite_id:"Water_1", price:"1g", group:"Aqua", volume:"500", demand:"30", exp:"144", extra:""}
    public function getProductProto(productId:String):ProductProto{
        var _resultObject:ProductProto = this._productPrototypesVector[int(productId.split("p")[1]) - 1];
        if(_resultObject == null){
            sendMessage(DebugMessage.ERROR_MESSAGE,"ProductPrototypeProxy: can't find productId:"+productId);
        }
        return _resultObject;
    }


    //---------------------------------------------
    //Shelf prototypes
    //---------------------------------------------

    public function get shelfPrototypes():Vector.<ShelfProto> {
        return this._shelfPrototypesVector;
    }
    public function set shelfsPrototype(value:Object):void {
        if(this._shelfPrototypesVector == null){
           this._shelfPrototypesVector = new <ShelfProto>[];
            var _protoIdPointer:int = 1;
            while(value["s_"+_protoIdPointer] != null){
                this._bufName = "s_"+_protoIdPointer;
                this._o = value[_bufName];
                this._shelfPrototypesVector.push(new ShelfProto(_bufName,_o.sprite_id,_o.price,_o.part_volume,_o.parts_num,_o.level,_o.occ_matrix));
                _protoIdPointer ++;
            }
        } else {
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: trying to reinitialize shhelfPrototypes")
        }
    }
    public function getShelfProto(shelfId:String):ShelfProto{
        var _resultObject:ShelfProto = this._shelfPrototypesVector[int(shelfId.split("_")[1]) - 1];
        if(_resultObject == null){
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: can't find shelfId:"+shelfId);
        }
        return _resultObject;
    }

    //---------------------------------------------
    //Shop Objects prototypes
    //---------------------------------------------

    public function get shopObjectPrototypes():Object {
        return this._shopObjectPrototypes;
    }
    public function set shopObjectPrototypes(value:Object):void {
        if(this._shopObjectPrototypes == null){
            this._shopObjectPrototypes = value;
        } else {
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: trying to reinitialize shopObjectPrototypes")
        }
    }
    public function getShopObjectProto(objectId:String):ShopObjectProto{
        var _resultObject:Object = this._shopObjectPrototypes[objectId];
        if(_resultObject == null){
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: can't find objectId:"+objectId);
        }
        return new ShopObjectProto(objectId,_resultObject.sprite_id,_resultObject.price,_resultObject.level,_resultObject.occ_matrix, _resultObject.disabled_sides);
    }


    //---------------------------------------------
    //Floors prototypes
    //---------------------------------------------

    public function set floorsPrototype(value:Object):void {
        if(this._floorsPrototypesVector == null){
            this._floorsPrototypesVector = new <ShopDesignItemProto>[];
            var _protoIdPointer:int = 1;
            while(value["f_"+_protoIdPointer] != null){
                this._bufName = "f_"+_protoIdPointer;
                this._o = value[_bufName];
                this._floorsPrototypesVector.push(new ShopDesignItemProto(this._bufName,_o.sprite_id,_o.cost, _o.level));
                _protoIdPointer ++;
            }
        } else {
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: trying to reinitialize floorsPrototype")
        }
    }

    /// floorId example: "f_1"
    public function getFloorProto(floorId:String):ShopDesignItemProto{
        var _resultObject:ShopDesignItemProto = this._floorsPrototypesVector[int(floorId.split("_")[1]) - 1];
        if(_resultObject == null){
            sendMessage(DebugMessage.ERROR_MESSAGE,"ProductPrototypeProxy: can't find floorID:"+floorId);
        }
        return _resultObject;
    }

    public function get floorsPrototypes():Vector.<ShopDesignItemProto> {
        return this._floorsPrototypesVector;
    }


    //---------------------------------------------
    //Walls prototypes
    //---------------------------------------------

    public function set wallsPrototype(value:Object):void {
        if(this._wallsPrototypesVector == null){
            this._wallsPrototypesVector = new <ShopDesignItemProto>[];
            var _protoIdPointer:int = 1;
            while(value["w_"+_protoIdPointer] != null){
                this._bufName = "w_"+_protoIdPointer;
                this._o = value[_bufName];
                this._wallsPrototypesVector.push(new ShopDesignItemProto(this._bufName,_o.sprite_id,_o.cost, _o.level));
                _protoIdPointer ++;
            }
        } else {
            sendMessage(DebugMessage.ERROR_MESSAGE,"PrototypeProxy: trying to reinitialize wallsPrototype")
        }
    }

    /// wallId example: "w_1"
    public function getWallProto(wallId:String):ShopDesignItemProto{
        var _resultObject:ShopDesignItemProto = this._wallsPrototypesVector[int(wallId.split("_")[1]) - 1];
        if(_resultObject == null){
            sendMessage(DebugMessage.ERROR_MESSAGE,"ProductPrototypeProxy: can't find wallId:"+wallId);
        }
        return _resultObject;
    }

    public function get wallsPrototypes():Vector.<ShopDesignItemProto> {
        return this._wallsPrototypesVector;
    }
}
}
